package com.taotao.web;

import com.taotao.EasyUIResult;
import com.taotao.SystemConstants;
import com.taotao.TaotaoResult;
import com.taotao.pojo.*;
import com.taotao.service.TbItemDescService;
import com.taotao.service.TbItemParamItemService;
import com.taotao.service.TbItemParamService;
import com.taotao.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author evader
 * @date 2020-09-22 17:27
 */
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private TbItemService itemService;

    @Autowired
    private TbItemDescService itemDescService;

    @Autowired
    private TbItemParamService tbItemParamService;

    @Autowired
    private TbItemParamItemService tbItemParamItemService;

    // 商品列表
    @RequestMapping("/list")
    @ResponseBody
    public EasyUIResult itemlist(@RequestParam("page") int pageNum, @RequestParam("rows") int pageSize){
        return itemService.getItemList(pageNum, pageSize);
    }

    @RequestMapping("/param/query/itemcatid/{cid}")
    @ResponseBody
    public TaotaoResult itemParam(@PathVariable Long cid){
        TaotaoResult result = tbItemParamService.selectByPrimaryKey(cid);
        return result;
    }

    // 商品描述
    @RequestMapping("/rest/item/query/item/desc/{id}")
    @ResponseBody
    public TbItemDesc itemdesc(@PathVariable Long id){
        return itemDescService.selectByPrimaryKey(id);
    }

    @RequestMapping("/query/cat/list")
    @ResponseBody
    public TbItemCat itemCat(){
        return new TbItemCat();
    }

    // 商品保存
    @RequestMapping("/save")
    @ResponseBody
    public TaotaoResult saveTbItem(TbItem tbItem, String desc,String itemParams) {
        TaotaoResult result = itemService.saveItem(tbItem, desc,itemParams);
        return result;
    }

    // 查询规格参数
    @RequestMapping("/query/{pid}")
    @ResponseBody
    public TaotaoResult query(@PathVariable Long pid,TbItemParamItemExample example){
        example.createCriteria().andItemIdEqualTo(pid);
        List<TbItemParamItem> tbItemParamItems = tbItemParamItemService.selectByExampleWithBLOBs(example);

        return TaotaoResult.ok(tbItemParamItems.get(0));
}


    @RequestMapping("/delete")
    @ResponseBody
    public TaotaoResult delete(@RequestParam("ids") List<Long> ids){
        itemService.operatingStatus(ids,SystemConstants.TAOTAO_ITEM_STATUS_DEL);
        return TaotaoResult.ok();
    }

    @RequestMapping("/instock")
    @ResponseBody
    public TaotaoResult instock(@RequestParam("ids") List<Long> ids){
        itemService.operatingStatus(ids,SystemConstants.TAOTAO_ITEM_STATUS_DOWN);
        return TaotaoResult.ok();
    }

    @RequestMapping("/reshelf")
    @ResponseBody
    public TaotaoResult reshelf(@RequestParam("ids") List<Long> ids){
        itemService.operatingStatus(ids,SystemConstants.TAOTAO_ITEM_STATUS_NORMAL);
        return TaotaoResult.ok();
    }

}
